﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ActionSystem.Core;
using ActionSystem.Core.ActiveLogic;
using ActionSystem.Core.PhysicsLogic;

namespace ActionSystem.Mono
{
    public abstract class MonoStateMachine : BaseStateMachine,IActiveLogicAbility,IPhysicsAbility
    {
        public override void ResetMachine()
        {
            base.ResetMachine();
            updateLogic = new MonoLogicUpdate(this);
            fixedUpdateLogic = new FixedUpdateLogic(this);
            if(HasStates)
            {
                Debug.LogError("MonoStateMachine不应该配置State");
            }
            SMStart();
        }

        #region UnityAPI
        public override void Awake()
        {
            base.Awake();
            ActionSystemManager.Instance.RegisterSM(this);
            SMAwake();
        }
        public virtual void SMAwake()
        {

        }
        public virtual void SMStart()
        {

        }
        #endregion
        #region Mono逻辑
        /// <summary>
        /// 对应 unity update
        /// </summary>
        /// <param name="deltaTime"></param>
        public abstract void SMUpdate(float deltaTime);
        public virtual void SMPostUpdate(float deltaTime) { }
        /// <summary>
        /// 对应 unity FixedUpdate
        /// </summary>
        /// <param name="fixedDeltaTime"></param>
        public abstract void SMFixedUpdate(float fixedDeltaTime);
        public virtual void SMPostFixedUpdate(float fixedDeltaTime) { }

        
        #endregion




        class MonoLogicUpdate : BaseActiveLogicAbility
        {
            MonoStateMachine stateMachine;
            public MonoLogicUpdate(BaseStateMachine baseStateMachine) : base(baseStateMachine)
            {
                stateMachine = baseStateMachine as MonoStateMachine;
            }
            public override void ActiveLogicActionUpdate()
            {
                stateMachine?.SMUpdate(stateMachine.LogicDeltaTime);
            }
            public override void PostActiveLogicActionUpdate()
            {
                stateMachine?.SMPostUpdate(stateMachine.LogicDeltaTime);
            }
        }
        
        BaseActiveLogicAbility updateLogic;
        public BaseActiveLogicAbility GetActiveLogicAbility()
        {
            return updateLogic;
        }
        
        BasePhysicsAbility fixedUpdateLogic;
        class FixedUpdateLogic : BasePhysicsAbility
        {
            MonoStateMachine stateMachine;
            public FixedUpdateLogic(BaseStateMachine baseStateMachine) : base(baseStateMachine)
            {
                stateMachine = baseStateMachine as MonoStateMachine;
            }
            public override void PreSimulationUpdate(float fixedDeltaTime)
            {
                stateMachine?.SMFixedUpdate(fixedDeltaTime);
            }
            public override void PostSimulationUpdate(float fixedDeltaTime)
            {
                stateMachine?.SMPostFixedUpdate(fixedDeltaTime);
            }
        }
        public BasePhysicsAbility GetPhysicsAbility()
        {
            return fixedUpdateLogic;
        }
        
    }
}


